/*
	epdll.c
	DLL implementation of Ethernet Powerlink v1.1.0
*/

#include "epdll.h"

UINT8 DLL_CS;
UINT8 DLL_MS;

UINT8 D_DLL_FeatureCN_BOOL;
UINT8 D_DLL_FeatureMN_BOOL;

SINT8 EPDLLCNInit(){
	DLL_CS = DLL_CS_NON_CYCLIC;
	D_DLL_FeatureCN_BOOL = TRUE;
	D_DLL_FeatureMN_BOOL = FALSE;
	/*Add any other DLL init code here*/
	return 0;
}

SINT8 EPDLLMNInit(){
	DLL_MS = DLL_MS_NON_CYCLIC;
	D_DLL_FeatureCN_BOOL = FALSE;
	D_DLL_FeatureMN_BOOL = TRUE;
	/*Add any other DLL init code here*/
	return 0;
}

SINT8 EPDLLUpdateState(UINT8 event){
	if (D_DLL_FeatureCN_BOOL == TRUE){ /*CN*/
		if (IS_NMT_GS_INITIALIZATION(NMT_CS) || (NMT_CS == NMT_CS_NOT_ACTIVE) || (NMT_CS == NMT_CS_BASIC_ETHERNET) || (NMT_CS == NMT_CS_PRE_OPERATIONAL_1)){
			DLL_CS = DLL_CS_NON_CYCLIC;
		}else if((NMT_CS == NMT_CS_PRE_OPERATIONAL_2) || (NMT_CS == NMT_CS_READY_TO_OPERATE) || (NMT_CS == NMT_CS_OPERATIONAL)){
			switch(DLL_CS){
			case DLL_CS_WAIT_SOC:
				switch(event){
				case DLL_CE_PREQ:
				case DLL_CE_PRES:
				case DLL_CE_SOA:
				case DLL_CE_ASND:
				case DLL_CE_SOC_TIMEOUT:
					DLL_CS = DLL_CS_WAIT_SOC;
					break;
				case DLL_CE_SOC:
					DLL_CS = DLL_CS_WAIT_PREQ;
					break;
				}
				break;
			case DLL_CS_WAIT_SOA:
				switch(DLL_CS){
				case DLL_CE_PRES:
				case DLL_CE_ASND:
					DLL_CS = DLL_CS_WAIT_SOA;
					break;
				case DLL_CE_SOC:
					DLL_CS = DLL_CS_WAIT_PREQ;
					break;
				case DLL_CE_SOA:
				case DLL_CE_PREQ:
				case DLL_CE_SOC_TIMEOUT:
					DLL_CS = DLL_CS_WAIT_SOC;
					break;
				}
				break;
			case DLL_CS_WAIT_PREQ:
				switch(DLL_CS){
				case DLL_CE_PRES:
				case DLL_CE_SOC:
				case DLL_CE_ASND:
					DLL_CS = DLL_CS_WAIT_PREQ;
					break;
				case DLL_CE_SOA:
				case DLL_CE_SOC_TIMEOUT:
					DLL_CS = DLL_CS_WAIT_SOC;
					break;
				case DLL_CE_PREQ:
					DLL_CS = DLL_CS_WAIT_SOA;
					break;
				}
				break;
			}
		}
	}else{ /*MN*/
		if (IS_NMT_GS_INITIALIZATION(NMT_MS) || (NMT_MS == NMT_MS_NOT_ACTIVE)){
			DLL_MS = DLL_MS_NON_CYCLIC;
		}else if(NMT_MS == NMT_MS_BASIC_ETHERNET){
			DLL_MS = DLL_MS_NON_CYCLIC;
		}else if(NMT_MS == NMT_MS_PRE_OPERATIONAL_1){
			switch(DLL_MS){
			case DLL_MS_WAIT_SOA:
				break;
			case DLL_MS_WAIT_ASND:
				break;
			}
		}
	}
	return 0;
}

SINT8 EPDLLPackageReceived(EPDLLBuffer* buffer, UINT16 length){
	return 0;
}
